package com.example.chatzsyproject.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.chatzsyproject.R;
import com.example.chatzsyproject.app.MyApplication;
import com.example.chatzsyproject.base.BaseActivity;
import com.example.chatzsyproject.callback.UpLoadOSSCallBack;
import com.example.chatzsyproject.config.Config;
import com.example.chatzsyproject.utils.HttpConfig;
import com.example.chatzsyproject.utils.HttpResult;
import com.example.chatzsyproject.utils.OkHttpClientUtils;
import com.example.chatzsyproject.utils.UploadHelper;
import com.example.chatzsyproject.utils.Utils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.exceptions.HyphenateException;
import com.lvfq.library.utils.LvToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class RegistActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_yzm)
    EditText et_yzm;
    @BindView(R.id.tv_yzm)
    Chronometer tv_yzm;
    @BindView(R.id.et_oldpassword)
    EditText et_oldpassword;
    @BindView(R.id.et_newpassword)
    EditText et_newpassword;
    @BindView(R.id.bt_regist)
    Button bt_regist;
    @BindView(R.id.im_head)
    ImageView im_head;
    ArrayList<String> photoList = new ArrayList<>();
    int REQUEST_IMAGE = 10010;
    String photoPath = "";
    @BindView(R.id.im_agree)
    ImageView im_agree;
    boolean isAgree = false;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (msg.arg1 == 203) {//重复注册
                    LvToastUtil.showToast(RegistActivity.this, "注册失败，当前账户已经注册了");
                } else {
                    LvToastUtil.showToast(RegistActivity.this, "注册失败，位置原因");
                }
            } else if (msg.what == 2) {
                LvToastUtil.showToast(RegistActivity.this, "注册成功");
                RegistActivity.this.finish();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        bt_regist.setOnClickListener(this);
        im_head.setOnClickListener(this);
        im_agree.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_agree:
                isAgree = !isAgree;
                if (isAgree) {
                    im_agree.setBackgroundResource(R.mipmap.choose_ture_bg);
                } else {
                    im_agree.setBackgroundResource(R.mipmap.choose_false_bg);
                }
                break;
            case R.id.im_head:
                MultiImageSelector.create(RegistActivity.this)
                        .showCamera(false)
                        .count(1)
                        .single()
                        .multi()
                        .origin(photoList)
                        .start(RegistActivity.this, REQUEST_IMAGE);
                break;
            case R.id.bt_regist:
                regist();
                break;
        }

    }

    public void regist() {
        String username = et_username.getText().toString();
        String pwd = et_newpassword.getText().toString();
        String oldpassword = et_oldpassword.getText().toString();
        if (photoPath.equals("")) {
            LvToastUtil.showToast(this, "请选择注册头像");
            return;
        }
        if (username.isEmpty()) {
            LvToastUtil.showToast(this, "用户手机号不能为空");
            return;
        }
        if (username.length() != 11 || !Utils.isMobile(username)) {
            LvToastUtil.showToast(this, "请输入正确的手机号码");
            return;
        }
        if (oldpassword.isEmpty()) {
            LvToastUtil.showToast(this, "请输入密码并且确认密码");
            return;
        }
        if (pwd.isEmpty()) {
            LvToastUtil.showToast(this, "请输入密码并且确认密码");
            return;
        }
        if (!isAgree) {
            LvToastUtil.showToast(this, "请同意该协议");
            return;
        }
        Map<String, String> map = new TreeMap<>();
        map.put("userhead", photoList.get(0));
        map.put("username", et_newpassword.getText().toString());
        map.put("password", pwd);
        map.put("email", "");
        map.put("mobile", username);
        map.put("code", et_yzm.getText().toString());
        OkHttpClientUtils.doPost(HttpConfig.register, map, new HttpResult() {
            @Override
            public void succ(String s) {

            }

            @Override
            public void error() {
                LvToastUtil.showToast(RegistActivity.this, Config.HttpErrorMessage);
            }

            @Override
            public void start() {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE) {
            if (resultCode == RESULT_OK) {
                photoList.clear();
                photoList.addAll(data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT));
                Glide.with(RegistActivity.this).load(photoList.get(0)).into(im_head);
                UploadHelper uploadHelper = new UploadHelper();
                uploadHelper.upload(photoList.get(0), new UpLoadOSSCallBack() {
                    @Override
                    public void ossSuccCallBack(String path) {
                        photoPath = path;
                    }
                });
            }
        }
    }
}
